package com.example.booklist.controller;

import com.example.booklist.model.UserInfo;
import com.example.booklist.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * ClassName: UserController
 * Package: IntelliJ IDEA
 * Description:
 *
 * @Author 谷京京
 * @Create 2023/10/30 17:26
 * @Version 1.0
 */
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public Boolean login(String userName, String password, HttpSession session) {
        if (!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return false;
        }
        UserInfo userInfo = userService.getUserInfoByName(userName);
        if(userInfo == null || userInfo.getId() <= 0) {
            return false;
        }
        if(password.equals(userInfo.getPassword())) {
            userInfo.setPassword("");
            session.setAttribute("session_user_key", userInfo);
            return true;
        }
        return false;
    }
}
